package com.efeichong.project.domain.entity.db;

import com.efeichong.audit.InsertDefault;
import com.efeichong.audit.UpdateDefault;
import com.efeichong.common.config.AuditorHolder;
import com.efeichong.common.domain.BaseEntity;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;


/**
 * @author
 * @date 2020/11/25
 * @description 用户
 */
@Setter
@Getter
@Entity
@Table(indexes = {
        @Index(name = "PRIMARY", columnList = "id", unique = true),
        @Index(name = "FK7a7mebmjq18nfvn60xbkrayyq", columnList = "create_by_id"),
        @Index(name = "idx_user_username", columnList = "username"),
})
//@LogicDelete(column = "hasDel")
//@DynamicInsert
//@DynamicUpdate
public class SysUser extends BaseEntity {
    /**
     * 名字
     */
    @Column(name = "[name]", columnDefinition = "varchar(30) not null comment'名字'")
    private String name;
    /**
     * 创建人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "[create_by_id]")
    @InsertDefault(val = AuditorHolder.class)
    private SysUser createBy;

    /**
     * 更新者
     */
    @UpdateDefault(val = AuditorHolder.class)
    @JoinColumn(name = "[update_by]")
    @ManyToOne(fetch = FetchType.LAZY)
    private SysUser updateBy;

    /**
     * 头像
     */
    @Column(name = "[avatar]", columnDefinition = "varchar(255)  comment'头像'")
    private String avatar;

    /**
     * 是否删除 0未删除 其它 已删除
     */
    @Column(name = "[has_del]", nullable = false, columnDefinition = "bigint default'0' comment'是否删除 0未删除'")
    private Long hasDel;

    /**
     * 密码
     */
    @Column(name = "[password]", columnDefinition = "varchar(255)  comment'密码'")
    private String password;

    /**
     * 号码
     */
    @Column(name = "[phone]", columnDefinition = "varchar(11) comment'号码'")
    private String phone;

    /**
     * 帐号状态（0停用 1正常）
     */
    @Column(name = "[status]", columnDefinition = "int default '0' comment '帐号状态（0停用 1正常）'")
    private Integer status;

    /**
     * 账号
     */
    @Column(name = "[username]", columnDefinition = "varchar(20)  comment'账号'")
    private String username;

    /**
     * 邮箱
     */
    @Column(name = "[email]", columnDefinition = "varchar(255)  comment'邮箱'")
    private String email;

    /**
     * 性别 0女 1男
     */
    @Column(name = "[gender]", columnDefinition = "int comment'帐号状态（性别 0女 1男）'")
    private Integer gender;

    /**
     * 是哪些部门的领导人
     */
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "leaders")
    private List<SysDept> leaderDepts;

    /**
     * 角色
     */
    @JoinTable(
            name = "sys_user_role_ship",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
    @ManyToMany(fetch = FetchType.LAZY)
    private List<SysRole> sysRoles = new ArrayList<>();

    /**
     * 身处哪些部门
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "sys_user_dept_ship",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "dept_id", referencedColumnName = "id"))
    private List<SysDept> sysDepts;

    /**
     * 上级
     */
    @ManyToOne(fetch = FetchType.LAZY)
    private SysUser parent;

    /**
     * 身处哪些岗位
     */
    @JoinTable(name = "sys_user_post_ship",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "post_id", referencedColumnName = "id"))
    @ManyToMany(fetch = FetchType.LAZY)
    private List<SysPost> sysPosts;

    /**
     * 所有父级
     */
    @JoinTable(name = "sys_user_user_ship",
            joinColumns = @JoinColumn(name = "id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "parent_id", referencedColumnName = "id"))
    @ManyToMany(fetch = FetchType.LAZY)
    private List<SysUser> userParents = new ArrayList<>();

    /**
     * 所有子集
     */
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "userParents")
    private List<SysUser> userChildren;

}
